Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor high quality texture import #72031

Merged

Conversation

reduz
Copy link
Member

@reduz reduz commented Jan 25, 2023

  • Only two texture import modes for low/high quality now:
    • S3TC/BPTC
    • ETC2/ASTC
  • Makes sense given this is the general preferred and most compatible combination of texture compression formats in most platforms.
  • Removed lossy_quality from VRAM texture compression options. It was unused everywhere.
  • Added a new "high_quality" option to texture import. When enabled, it uses BPTC/ASTC (BC7/ASTC4x4) instead of S3TC/ETC2 (DXT1-5/ETC2,ETCA).
  • Changed MacOS export settings so required texture formats depend on the architecture selected.
  • Also did some fixes in ASTC support (mipmaps not supported).

This solves the following problems:

  • Makes it simpler to import textures as high quality, without having to worry about the specific format used.
  • On Apple, compressed texture format support is very difficult both for the editor and for exporting because, depending on the hardware, its either one or the other. This makes exporting and running the editor on MacOS "Just Work"(tm).
  • As the editor can now run on platforms such as web, Mac OS with Apple Silicion and Android, it should no longer be assumed that S3TC/BPTC is available by default for it.

@reduz reduz requested review from a team as code owners January 25, 2023 11:23
@reduz reduz force-pushed the change-high-quality-texture-import branch from 3069bce to 04b31d5 Compare January 25, 2023 11:41
core/os/os.cpp Show resolved Hide resolved
@reduz reduz force-pushed the change-high-quality-texture-import branch from 04b31d5 to 6097a47 Compare January 25, 2023 12:02
@akien-mga akien-mga added this to the 4.0 milestone Jan 25, 2023
@reduz reduz force-pushed the change-high-quality-texture-import branch 2 times, most recently from 5bae6e5 to dc5cdf4 Compare January 25, 2023 15:13
@clayjohn
Copy link
Member

I'm getting a crash when loading a scene.

I deleted the .godot folder before trying to import. Then after crashing the first time I deleted the .import files as well and I received the same crash (albeit pointing to a different line number in ResourceImporterTexture::import

ERROR: Trying to unreference a SafeRefCount which is already zero is wrong and a symptom of it being misused.
Upon a SafeRefCount reaching zero any object whose lifetime is tied to it, as well as the ref count itself, must be destroyed.
Moreover, to guarantee that, no multiple threads should be racing to do the final unreferencing to zero.
   at: _check_unref_sanity (./core/templates/safe_refcount.h:176)


================================================================
handle_crash: Program crashed with signal 4
Engine version: Godot Engine v4.0.beta.custom_build (dc5cdf4de4aa003de9e78780f87d7264aa4f9ec1)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7fd112c42520] (??:0)
[2] StringName::unref() (/home/clayjohn/dev/godot/./core/templates/safe_refcount.h:173)
[3] StringName::~StringName() (/home/clayjohn/dev/godot/./core/string/string_name.h:182)
[4] ResourceImporterTexture::import(String const&, String const&, HashMap<StringName, Variant, HashMapHasherDefault, HashMapComparatorDefault<StringName>, DefaultTypedAllocator<HashMapElement<StringName, Variant> > > const&, List<String, DefaultAllocator>*, List<String, DefaultAllocator>*, Variant*) (/home/clayjohn/dev/godot/editor/import/resource_importer_texture.cpp:454)
[5] EditorFileSystem::_reimport_file(String const&, HashMap<StringName, Variant, HashMapHasherDefault, HashMapComparatorDefault<StringName>, DefaultTypedAllocator<HashMapElement<StringName, Variant> > > const*, String const&) (/home/clayjohn/dev/godot/editor/editor_file_system.cpp:2001)
[6] EditorFileSystem::_reimport_thread(unsigned int, EditorFileSystem::ImportThreadData*) (/home/clayjohn/dev/godot/editor/editor_file_system.cpp:2159)
[7] WorkerThreadPool::GroupUserData<EditorFileSystem, void (EditorFileSystem::*)(unsigned int, EditorFileSystem::ImportThreadData*), EditorFileSystem::ImportThreadData*>::callback_indexed(unsigned int) (/home/clayjohn/dev/godot/./core/object/worker_thread_pool.h:152)
[8] WorkerThreadPool::_process_task(WorkerThreadPool::Task*) (/home/clayjohn/dev/godot/core/object/worker_thread_pool.cpp:72)
[9] WorkerThreadPool::_native_low_priority_thread_function(void*) (/home/clayjohn/dev/godot/core/object/worker_thread_pool.cpp:165)
[10] Thread::callback(Thread*, Thread::Settings const&, void (*)(void*), void*) (/home/clayjohn/dev/godot/core/os/thread.cpp:67)
[11] void std::__invoke_impl<void, void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*>(std::__invoke_other, void (*&&)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*&&, Thread::Settings&&, void (*&&)(void*), void*&&) (/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/invoke.h:61)
[12] std::__invoke_result<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*>::type std::__invoke<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*>(void (*&&)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*&&, Thread::Settings&&, void (*&&)(void*), void*&&) (/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/invoke.h:96)
[13] void std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> >::_M_invoke<0ul, 1ul, 2ul, 3ul, 4ul>(std::_Index_tuple<0ul, 1ul, 2ul, 3ul, 4ul>) (/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/std_thread.h:252)
[14] std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> >::operator()() (/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/std_thread.h:259)
[15] std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> > >::_M_run() (/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/std_thread.h:210)
[16] /home/clayjohn/dev/godot/bin/godot.linuxbsd.editor.dev.x86_64.llvm(+0xb2089f3) [0x5651a18e79f3] (msdf-error-correction.cpp:?)
[17] /lib/x86_64-linux-gnu/libc.so.6(+0x94b43) [0x7fd112c94b43] (??:0)
[18] /lib/x86_64-linux-gnu/libc.so.6(+0x126a00) [0x7fd112d26a00] (??:0)
-- END OF BACKTRACE --
============================

@reduz
Copy link
Member Author

reduz commented Jan 27, 2023

@clayjohn The crash and error seem unrelated to the PR, which is very strange. This PR forces the reimport of all textures, so it may be related to that?

@reduz reduz force-pushed the change-high-quality-texture-import branch from dc5cdf4 to 5ad04e1 Compare January 27, 2023 10:04
@clayjohn
Copy link
Member

clayjohn commented Jan 27, 2023

@clayjohn The crash and error seem unrelated to the PR, which is very strange. This PR forces the reimport of all textures, so it may be related to that?

I'm not sure. I am testing on https://github.com/Rytelier/Godot-4-forest-benchmark and I can reliably reproduce the crash if I delete the .godot folder, or if import textures from an older version (I tried with Beta 15) and then try to import with this PR. Of note, after crashing, if I restart the editor it finishes importing the rest of the textures. So it seems like the issue could be due to the number of textures in the project?

I also tested with https://github.com/gdquest-demos/godot-4-3d-third-person-controller and it did not crash

Edit: Turns out in my testing project I had left "Import ETC" disabled. If I enable "Import ETC" I can reproduce the crash on master, so it is not caused by this PR

@clayjohn
Copy link
Member

Couple notes:

  1. When compressing Icon.png into ASTC I get the following error
astcenc: Encoding image size 64x64 to format ASTC_4x4, with mipmaps.
ERROR: Expected Image data size of 64x64x1 (with 6 mipmaps) = 5488 bytes, got 4096 bytes instead.
   at: initialize_data (core/io/image.cpp:2206)

It looks like the image size validator doesn't properly account for ASTC
2. I was able to work around the crash above by setting editor/import/use_multiple_threads to false. The crash happens more frequently when ETC ASTC is enabled. But it also happens on master with both S3TC and ETC2 enabled
3. The ASTC formats are missing from Image::format_names[]

godot/core/io/image.cpp

Lines 79 to 81 in a43db5a

"ETC2_RA_AS_RG",
"FORMAT_DXT5_RA_AS_RG",
};

Need to add them to the end:

"ASTC_4x4",
"ASTC_4x4_HDR",
"ASTC_8x8",
"ASTC_8x8_HDR",

@clayjohn
Copy link
Member

clayjohn commented Jan 28, 2023

Here is the error log from running on an android device:

01-27 17:26:57.152 30718 30753 E godot   : USER ERROR: Unable to open file: res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.astc.ctex.
01-27 17:26:57.152 30718 30753 E godot   :    at: _load_data (scene/resources/texture.cpp:846)
01-27 17:26:57.152 30718 30753 E godot   : USER ERROR: Failed loading resource: res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.astc.ctex. Make sure resources have been imported by opening the project in the editor at least once.
01-27 17:26:57.152 30718 30753 E godot   :    at: _load (core/io/resource_loader.cpp:222)
01-27 17:26:57.152 30718 30753 E godot   : USER ERROR: Failed loading resource: res://icon.svg. Make sure resources have been imported by opening the project in the editor at least once.
01-27 17:26:57.152 30718 30753 E godot   :    at: _load (core/io/resource_loader.cpp:222)
01-27 17:26:57.152 30718 30753 E godot   : USER ERROR: Can't load dependency: res://icon.svg.
01-27 17:26:57.152 30718 30753 E godot   :    at: load (core/io/resource_format_binary.cpp:696)
01-27 17:26:57.152 30718 30753 E godot   : USER ERROR: Failed loading resource: res://.godot/exported/133200997/export-3070c538c03ee49b7677ff960a3f5195-main.scn. Make sure resources have been imported by opening the project in the editor at least once.
01-27 17:26:57.152 30718 30753 E godot   :    at: _load (core/io/resource_loader.cpp:222)
01-27 17:26:57.152 30718 30753 E godot   : USER ERROR: Failed loading scene: res://main.tscn
01-27 17:26:57.152 30718 30753 E godot   :    at: start (main/main.cpp:2921)
01-27 17:26:57.153 30718 30753 E godot   : USER ERROR: Condition "default_certs != nullptr" is true.
01-27 17:26:57.153 30718 30753 E godot   :    at: load_default_certificates (modules/mbedtls/crypto_mbedtls.cpp:302)

Running on a Pixel 4. The scene is just the default icon on a Sprite2D taking up most of the screen. The icon is set to use high quality import. and only "ETC ASTC" import is enabled in the project settings (edit: same results if both "S3TC BPTC" and "ETC ASTC" enabled in project settings)
ASTC-test.zip

edit edit: Tested on Vulkan backend, same result. The project won't load and I get the above error endlessly. Unchecking the "high quality" flag on texture import makes the project work

@reduz reduz force-pushed the change-high-quality-texture-import branch 2 times, most recently from eeb9353 to 12c6d5f Compare January 28, 2023 17:26
@reduz
Copy link
Member Author

reduz commented Jan 28, 2023

@clayjohn Fixed some problems in the ASTC implementation, so hoping it works now. I can't manage to load ASTC on desktop (Godot does not recognize the format for some reason).

@clayjohn
Copy link
Member

@clayjohn Fixed some problems in the ASTC implementation, so hoping it works now. I can't manage to load ASTC on desktop (Godot does not recognize the format for some reason).

Same error as before. The file it is referencing exists, its almost like its not getting included in the APK though. Do we have logic that selects what imported resources are included in the APK?

01-28 10:35:36.267 11948 11980 E godot   : USER ERROR: Unable to open file: res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.astc.ctex.
01-28 10:35:36.267 11948 11980 E godot   :    at: _load_data (scene/resources/texture.cpp:846)
01-28 10:35:36.267 11948 11980 E godot   : USER ERROR: Failed loading resource: res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.astc.ctex. Make sure resources have been imported by opening the project in the editor at least once.

* Only two texture import modes for low/high quality now:
  * S3TC/BPTC
  * ETC2/ASTC
* Makes sense given this is the general preferred and most compatible combination in most platforms.
* Removed lossy_quality from VRAM texture compression options. It was unused everywhere.
* Added a new "high_quality" option to texture import. When enabled, it uses BPTC/ASTC (BC7/ASTC4x4) instead of S3TC/ETC2 (DXT1-5/ETC2,ETCA).
* Changed MacOS export settings so required texture formats depend on the architecture selected.

This solves the following problems:

* Makes it simpler to import textures as high quality, without having to worry about the specific format used.
* As the editor can now run on platforms such as web, Mac OS with Apple Silicion and Android, it should no longer be assumed that S3TC/BPTC is available by default for it.
@reduz reduz force-pushed the change-high-quality-texture-import branch from 142969f to 28f51ba Compare January 30, 2023 14:53
@reduz reduz requested a review from a team as a code owner January 30, 2023 14:53
@reduz
Copy link
Member Author

reduz commented Jan 30, 2023

@clayjohn Okay that took me a couple of hours to figure out, but it should be working well now. Make sure to test, but I tested on AMD (which supports ASTC) and it works fine.

Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I would merge ASAP to get it properly tested in beta 17, as I expect bugs on different platforms :)

I'll wait to see if @clayjohn can at least confirm it works on his Android device.

@reduz
Copy link
Member Author

reduz commented Jan 30, 2023

@akien-mga I think its safe to merge at this point, seems to work well enough for me.

@clayjohn
Copy link
Member

clayjohn commented Jan 30, 2023

Just tested on my android device and I'm still getting the same output as before

vulkan

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. After re-importing and re-exporting with the newest changes this seems to work on Android both in mobile and gl_compatibility

@jcostello
Copy link
Contributor

jcostello commented Jan 30, 2023

Dumb question but this has a visual impact?

@clayjohn
Copy link
Member

Dumb question but this has a visual impact?

Yes, same as switching from s3tc to bptc in current betas. There won't be a visual change in the default case though

@akien-mga akien-mga merged commit e9de988 into godotengine:master Jan 30, 2023
@akien-mga
Copy link
Member

Thanks!

akien-mga added a commit to akien-mga/godot that referenced this pull request Feb 23, 2023
This is now required after godotengine#72031 when using HDRs.

Could have further cleanup as I think these import options may not be needed
at all anymore, and etc/etc2 support doesn't seem to make much sense.
Likewise, the hardcoded "s3tc" in `get_platform_features` could maybe be
removed. But this is material for after 4.1.

Fixes godotengine#73789.
JeffVenancius pushed a commit to JeffVenancius/godot that referenced this pull request Mar 3, 2023
This is now required after godotengine#72031 when using HDRs.

Could have further cleanup as I think these import options may not be needed
at all anymore, and etc/etc2 support doesn't seem to make much sense.
Likewise, the hardcoded "s3tc" in `get_platform_features` could maybe be
removed. But this is material for after 4.1.

Fixes godotengine#73789.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants